package com.serverlite.system.main.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;

import com.serverlite.framework.servlet.core.WebPage;
import com.serverlite.framework.servlet.mapping.Controller;
import com.serverlite.framework.servlet.mapping.RequestMapping;
import com.serverlite.system.config.AppConfig;
import com.serverlite.system.module.service.ModuleService;
import com.serverlite.system.user.service.UserService;

/**
 * 主框架界面
 * @author Neo
 *
 */
@Controller(value="/system/main")
public class MainController {
    
    /**
     * 主框架页面 
     */
    @RequestMapping(value="/frame")
    @SuppressWarnings("unchecked")
    public WebPage frame(HttpServletRequest requset){
        Map<String,Object> o = (Map<String, Object>) requset.getSession().getAttribute(AppConfig.USER_SESSION_ID);
        if(UserService.isAdmin((String)o.get("username"), (String)o.get("password"))){
            requset.setAttribute("isAdmin", true);
        }
        requset.setAttribute("user", o);
        return new WebPage().forward("/system/frame.jsp");
    }
    
    /**
     * 登录页面 
     */
    @RequestMapping(value="/login")
    public WebPage login(){
        return new WebPage().forward("/system/login.jsp");
    }
    
    /**
     * 注销方法 
     */
    @RequestMapping(value="/logout")
    public WebPage logout(HttpServletRequest request){
        request.getSession().removeAttribute(AppConfig.USER_SESSION_ID);
        return login();
    }
    
    /**
     * 验证登录 
     */
    @RequestMapping(value="/validate")
    public boolean validate(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Map<String,Object> o = UserService.login(username, password);
        if(o != null){
            request.getSession().setAttribute(AppConfig.USER_SESSION_ID, o);
            return true;
        }
        return false;
    }
    
    /**
     * 加载模块树
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value="/moduletree")
    public JSONArray moduletree(HttpServletRequest requset){
        Map<String,Object> o = (Map<String, Object>) requset.getSession().getAttribute(AppConfig.USER_SESSION_ID);
        if(UserService.isAdmin((String)o.get("username"), (String)o.get("password"))){
            return ModuleService.adminTree("-1");
        } else {
            String roleIds = (String)o.get("role_ids");
            return ModuleService.permissionTree("-1", roleIds);
        }
    }
    
    /**
     * 修改密码界面 
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value="/usersetting")
    public WebPage usersetting(HttpServletRequest request){
        Map<String,Object> o = (Map<String, Object>) request.getSession().getAttribute(AppConfig.USER_SESSION_ID);
        if(o != null){
            request.setAttribute("vo", o);
        }
        return new WebPage().forward("/system/usersetting.jsp");
    }
}
